const proxyConfig = require('./proxyConfig')
console.log(proxyConfig)
const proxy = buildProxy(proxyConfig)
module.exports = {
  port: process.env.VUE_APP_PORT || 8080,
  headers: {
    'Access-Control-Allow-Origin': '*' // 主应用获取子应用时跨域响应头
  },
  overlay: {
    warnings: false
  },
  proxy
}

/**
 * 根据proxyConfig 生成proxy
 * @param {Array} proxyConfig
 *
 */
function buildProxy (proxyConfig) {
  const proxy = {}
  proxyConfig.forEach(config => {
    let pathRewrite = {}
    if (config.pathRewrite === undefined) {
      pathRewrite[`^/${config.key}`] = ''
    } else {
      pathRewrite = config.pathRewrite
    }
    proxy[`/${config.key}`] = {
      target: config.target,
      changeOrigin: true,
      pathRewrite
    }
  })
  return proxy
}
